package com.spark.rectify

import android.app.Activity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.spark.common.BaseFragment
import kotlinx.android.synthetic.main.frag_execute_info.*
import org.jetbrains.anko.*

/**
 * 执行情况
 */
class ExecuteInfoFragment : BaseFragment() {
    private lateinit var executeInfoAdapter: ExecuteInfoAdapter

    companion object {
        fun newInstance():ExecuteInfoFragment {
            val target = ExecuteInfoFragment()
            val args = Bundle()

            target.arguments = args
            return target
        }
    }
    override fun contentResId(): Int {
        return R.layout.frag_execute_info
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recy_execute_infos.apply {
            executeInfoAdapter = ExecuteInfoAdapter(R.layout.item_execute_info)
            adapter = executeInfoAdapter
            layoutManager = LinearLayoutManager(context)
        }
    }
}

data class ExecuteInfoModel(
        var id: String = "",
        var name: String = "",
        var state: String = ""
)

class ExecuteInfoAdapter(layoutRes: Int) : BaseQuickAdapter<ExecuteInfoModel, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: ExecuteInfoModel?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_name, item.name)
            setText(R.id.tv_state, item.state)
        }
    }

}

class ExecuteInfoItemUI(var executeItemName: String, var executeState: String) : AnkoComponent<Activity> {
    fun bind(name: String, state: String) {
        executeItemName = name
        executeState = state
    }

    override fun createView(ui: AnkoContext<Activity>): View = with(ui) {
        relativeLayout {
            lparams(matchParent, wrapContent)

            var tvExecuteItem = textView(executeItemName) {
                textSize = dip(14).toFloat()
                textColor = R.color.color333
            }.lparams {
                centerVertically()
                padding = R.dimen.border_padding
            }

            textView(executeState).lparams {
                centerVertically()
                alignParentEnd()
                padding = R.dimen.border_padding
            }

            include<View>(R.layout.layout_line_divider).lparams {
                leftMargin = R.dimen.border_padding
                below(tvExecuteItem)
                topMargin = R.dimen.margin_10
                rightMargin = R.dimen.border_padding
            }
        }
    }

}